const {spawnSync, spawn} = require("node:child_process");
const chromePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
const firefoxPath = "/Applications/Firefox.app/Contents/MacOS/firefox"
const wechatwebdevtoolsPath = "/Applications/wechatwebdevtools.app/Contents/MacOS/cli"
///项目路径
const projectPath = (() => {
    let cwd = process.argv[1].split("/").filter((item) => {
        return item.length
    })
    cwd.pop()
    return "/" + cwd.join("/")
})();
const chrome = {
    command: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
}
const firefox = {
    command: "/Applications/Firefox.app/Contents/MacOS/firefox"
}
const wechat = {
    command: "/Applications/wechatwebdevtools.app/Contents/MacOS/cli",
    path: projectPath + "/dist/dev/mp-weixin"
}

/*
*   "nodePath": "node路径",
    "scriptPath": "本脚本路径",
    "platform": "web/app/minApp",
    "secondaryPlatform": "web:chrome/firefox,app:iOS/Android,minApp:wx",
    "path": "web:网址可以为空,app:编译产物路径,minApp:编译产物路径"
* */
class ParameterAnalysis {
    nodePath = ""
    scriptPath = ""
    platform = ""
    secondaryPlatform = ""
    path = ""

    constructor(arg) {
        this.nodePath = arg[0]
        this.scriptPath = arg[1]
        this.platform = arg[2]
        this.secondaryPlatform = arg[3]
        this.path = arg[4]
    }

    runSpawn(command, args, options, callback) {
        let ls = spawn(command, args, options)
        ls.stdout.on('data', (data) => {
            let dataString = data.toString()
            callback("stdout", dataString)
        });

        ls.stderr.on('data', (data) => {
            let dataString = data.toString()
            callback("stderr", dataString)
        });
        ls.on('close', (code) => {
            callback("close", code.toString())
        });
    }

    run() {
        switch (this.platform) {
            case "web":
                this.runWeb()
                break
            case "app":
                console.log("还未实现")
                break
            case "minApp":
                this.runMinApp()
                break
        }
    }

    runWeb() {
        this.runSpawn("yarn", ["uni"], {
            cwd: projectPath
        }, (type, data) => {
            console.log(data)
            const reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g
            const urls = data.match(reg)
            if (urls != null && urls.length > 0) {
                const newArray = urls.filter((item) => {
                    if (item.includes("http://localhost:")) {
                        return item
                    }
                    return null
                })
                if (newArray.length > 0) {
                    if (this.secondaryPlatform === "firefox") {
                        spawnSync(firefox.command, ["--new-tab", newArray[0]])
                    } else {
                        spawnSync(chrome.command, newArray[0])
                    }
                }
            }
        })
    }

    runMinApp() {
        this.runSpawn("yarn", ["uni", "-p", "mp-weixin"], {
            cwd: projectPath
        }, (type, data) => {
            console.log(data)
            if (data.includes("ready in")) {
                const path = projectPath + wechat.path
                spawnSync(wechat.command, ["open", "--project", wechat.path])
            }
        })
    }
}

console.log(process.argv)
if (process.argv.length < 4) {
    console.log("参数有误")
    process.exit(-1)
}
const parameterAnalysis = new ParameterAnalysis(process.argv)

parameterAnalysis.run()


// runSpawn("yarn", ["run", "uni"], {
//     cwd: "/Users/mac/汪明个人资料/uniapp_gree_ctm/gree_ctm"
// }, (type,dataString) => {
//     console.log(type+dataString)
// })

// runSpawn(firefoxPath, ["--new-tab","www.baidu.com"], {},(type,dataString) => {
//     console.log(type+dataString)
// })

// let ls = spawn("yarn", ["run", "uni"], {cwd: "/Users/mac/汪明个人资料/uniapp_gree_ctm/gree_ctm"})
//
//
// ls.stdout.on('data', (data) => {
//     let dataString = data.toString()
//     var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
//     console.log(dataString)
//     console.log(dataString.match(reg))
//
// });
//
// ls.stderr.on('data', (data) => {
//
//     console.error(`stderr: ${data}`);
// });
//
// ls.on('close', (code) => {
//     console.log(`child process exited with code ${code}`);
// });

